<?php
namespace Home\Controller;

use Think\Controller;
use Home\Event\JssdkEvent;
use Think\Exception;
class TestController extends Controller {

    public function index(){
        $ak = C('BAIDU_AK');
        $jssdk = A('Jssdk','Event');
        $signPackage = $jssdk->GetSignPackage();
        //查询店铺所属区域传到页面上
        $this->assign(['signPackage' => $signPackage]);
        $this->assign(['ak' => $ak]);
        $this->display();
    }

    public function jwd(){

        echo $this->getDistance('106.555301', '29.612736', '106.555435', '29.607594', 2);
    }

    public function ceshi1(){
        $model = M();
        try {
            //开启事物
            $model->startTrans();
            $status = $this->ceshi2(true);
            dump($status);
            if($status === false){
                throw new Exception(false);
            }
            $model->commit();
        }catch (\Exception $e){
            $model->rollback();
            dump('error');
        }
    }
    public function ceshi2($v){
        $status = M('b2c_order_num')->where(['id'=>15])->save(['status'=>$v]);
        //echo M()->_sql();
        return $status;
    }

    /**
     * 经纬度算距离
     * @param $longitude1
     * @param $latitude1
     * @param $longitude2
     * @param $latitude2
     * @param int $unit
     * @param int $decimal 1表示米，2千米
     * @return float
     */
    function getDistance($longitude1, $latitude1, $longitude2, $latitude2, $unit=2, $decimal=2){

        $EARTH_RADIUS = 6370.996; // 地球半径系数
        $PI = 3.1415926;

        $radLat1 = $latitude1 * $PI / 180.0;
        $radLat2 = $latitude2 * $PI / 180.0;

        $radLng1 = $longitude1 * $PI / 180.0;
        $radLng2 = $longitude2 * $PI /180.0;

        $a = $radLat1 - $radLat2;
        $b = $radLng1 - $radLng2;

        $distance = 2 * asin(sqrt(pow(sin($a/2),2) + cos($radLat1) * cos($radLat2) * pow(sin($b/2),2)));
        $distance = $distance * $EARTH_RADIUS * 1000;

        if($unit==2){
            $distance = $distance / 1000;
        }

        return round($distance, $decimal);

    }

    public function info(){
        echo 123;
        phpinfo();
    }

    public function asdf(){

//        echo bcmul(0.1,0.01,3);
//        shoppingAward('16358', 25, '170302232069121');
//        WeixinPayPushToFatherMember(8738, 25, '170302232069121');
//        $aa = WeixinAttentionToFatherMember(25, 'oXj1Ew_ZwBdqPSdkXG0wlZPrm0gs', '大猩猩', '10');
//        $aa = WeixinSonMemberCreatePosterToFatherMember(25, 'oXj1Ew_ZwBdqPSdkXG0wlZPrm0gs', '大猩猩', '10');
        $aa = WeixinOrderOver(25, 'oXj1Ew_ZwBdqPSdkXG0wlZPrm0gs', '大猩猩', '10', 44, 5);
        var_dump($aa);
    }
}